<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;

use app\common\logic\Advertisement as CommonAdvertisement;

/**
 * 广告接口逻辑
 */
class Advertisement extends ApiBase
{
    
    public static $commonAdvertisementLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonAdvertisementLogic) && static::$commonAdvertisementLogic = new CommonAdvertisement();
    }
    
    /**
     * 获取广告列表
     * @author 王勇程 <laughskydragon@qq.com>
     * @param array $data
     * @return array
     */
    public function getAdvertisementList($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }

        // 2. 调用getWhere方法
        $where = static::$commonAdvertisementLogic->getWhere($data);

        // 3. 调用common层业务逻辑
        $result = static::$commonAdvertisementLogic->getAdvertisementList($where);

        return $result;
    }
    
    /**
     * 获取首页广告列表
     * @author 王勇程 <laughskydragon@qq.com>
     * @param array $data
     * @return array
     */
    public function getHomeAdvertisementList($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }

        // 2. 调用getWhere方法
        $where = static::$commonAdvertisementLogic->getWhere($data);

        // 3. 调用common层业务逻辑
        $result = static::$commonAdvertisementLogic->getHomeAdvertisementList($where);

        return $result;
    }
    
    /**
     * 记录广告点击
     */
    public function recordAdvertisementClick($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'] ?? 0;
        }
        
        // 调用common层业务逻辑
        $result = static::$commonAdvertisementLogic->recordAdvertisementClick($data);
        
        // 处理common层返回的[状态码, 消息, 数据]格式
        if (is_array($result) && isset($result[0])) {
            if ($result[0] == RESULT_SUCCESS) {
                return isset($result[2]) ? $result[2] : '记录成功';
            } else {
                return $this->returnError($result[1] ?? '记录失败');
            }
        }
        
        return $result;
    }
}